import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/core/integral-grade',
    component: Layout,
    redirect: '/core/integral-grade/list',
    name: 'integralGrade',
    meta: { title: '积分等级管理', icon: 'el-icon-s-help' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'integralGradeList',
        component: () => import('@/views/core/integral-grade/list'),
        meta: { title: '积分等级列表', icon: 'table' }
      },
      {
        path: 'save',
        name: 'integralGradeSave',
        component: () => import('@/views/core/integral-grade/save'),
        meta: { title: '积分等级添加', icon: 'tree' }
      }
    ]
  },
  {
    path: '/core/dict',
    component: Layout,
    redirect: '/core/dict/list',
    name: 'dict',
    meta: { title: '字典管理', icon: 'el-icon-s-help' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'dictList',
        component: () => import('@/views/core/dict/list'),
        meta: { title: '字典列表', icon: 'table' }
      }
    ]
  },
  {
    path: '/core/user-info',
    component: Layout,
    redirect: '/core/user-info/list',
    name: 'userInfo',
    meta: { title: '会员管理', icon: 'el-icon-s-help' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'userList',
        component: () => import('@/views/core/user-info/list'),
        meta: { title: '会员列表', icon: 'table' }
      }
    ]
  },
  {
    path: '/core/borrower',
    component: Layout,
    redirect: '/core/borrower/list',
    name: 'borrower',
    meta: { title: '借款管理', icon: 'el-icon-s-help' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'borrowerList',
        component: () => import('@/views/core/borrower/list'),
        meta: { title: '借款人列表', icon: 'table' }
      },
      {
        path: 'detail',
        name: 'borrowerDetail',
        hidden: true,
        component: () => import('@/views/core/borrower/detail'),
        meta: { title: '借款人详情', icon: 'table' }
      },
      {
        path: 'info-list',
        name: 'borrowerInfoList',
        component: () => import('@/views/core/borrow-info/list'),
        meta: { title: '借款列表', icon: 'table' }
      },
      {
        path: 'info-detail/:id',
        name: 'borrowerInfoDetail',
        hidden: true,
        component: () => import('@/views/core/borrow-info/detail'),
        meta: { title: '借款详情', icon: 'table' }
      },
    ]
  },
  {
    path: '/core/lend',
    component: Layout,
    name: 'coreLend',
    meta: { title: '标的管理', icon: 'el-icon-s-flag' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'coreLendList',
        component: () => import('@/views/core/lend/list'),
        meta: { title: '标的列表' }
      },
      {
        path: 'detail/:id',
        name: 'coreLendDetail',
        component: () => import('@/views/core/lend/detail'),
        meta: { title: '标的详情' },
        hidden: true
      }
    ]
  },
]

export const asyncRoutes = [

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
